using Microsoft.EntityFrameworkCore;
using Zhaoxi.MSACommerce.OrderService.Core.Enums;
using Zhaoxi.MSACommerce.OrderService.Infrastructure.Data.Configuration;
using ZhaoXi.MSACommerce.SharedKenel.Messaging;

namespace Zhaoxi.MSACommerce.OrderService.UseCases.Commands;

public record UpdateOrderStatusCommand(long OrderId, OrderStatus Status) : ICommand<Result>;


public class UpdateOrderStatusHandler(OrderDbContext dbContext) : ICommandHandler<UpdateOrderStatusCommand, Result>
{
    public async Task<Result> Handle(UpdateOrderStatusCommand request, CancellationToken cancellationToken)
    {
        var order = await dbContext.Orders
            .Include(o=>o.OrderInfo)
            .Where(o=>o.Id == request.OrderId)
            .FirstOrDefaultAsync(cancellationToken);
        
        if (order == null)return Result.NotFound();
        
        order.OrderInfo.Status = request.Status;
        
        await dbContext.SaveChangesAsync(cancellationToken);
        
        return Result.Success();
    }
}